//
//  ZBBListenNewsViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "ZBBListenNewsViewController.h"
#import "TwoToOne.h"
#import "FitHeader.h"
#import "ZBBListenNewsTitleCell.h"
#import "ZBBContentListenNewsOrdinaryCell.h"
#import "ZBBContentListenNewsRecomendCell.h"
#import "TabBarController.h"
@interface ZBBListenNewsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *titleCollectionView;
@property(nonatomic,strong)UICollectionView *contentCollectionView;
@property(nonatomic, strong)NSMutableArray *titleSourceArray;

@end

@implementation ZBBListenNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    
    
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *titleFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    titleFlowLayout.minimumInteritemSpacing = 0;
    titleFlowLayout.minimumLineSpacing = 0;
     titleFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    titleFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _titleCollectionView.showsHorizontalScrollIndicator = NO;
    _titleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 60 * FITWidth, 40 * FITHeight) collectionViewLayout:titleFlowLayout];
   [_titleCollectionView registerClass:[ZBBListenNewsTitleCell class] forCellWithReuseIdentifier:@"title"];
    _titleCollectionView.pagingEnabled = NO;
    _titleCollectionView.dataSource = self;
    _titleCollectionView.delegate = self;
    _titleCollectionView.layer.borderWidth = 0.5;
    _titleCollectionView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _titleCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_titleCollectionView];

    UICollectionViewFlowLayout *contentFlowlayout = [[UICollectionViewFlowLayout alloc] init];

    contentFlowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    contentFlowlayout.minimumInteritemSpacing = 0;
    contentFlowlayout.minimumLineSpacing = 0;
    contentFlowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50 * FITHeight, self.view.frame.size.width, self.view.frame.size.height - 64 - 40 * FITHeight) collectionViewLayout:contentFlowlayout];
    [self.view addSubview:_contentCollectionView];
    _contentCollectionView.delegate = self;
    _contentCollectionView.dataSource = self;
    _contentCollectionView.pagingEnabled = YES;
    [_contentCollectionView registerClass:[ZBBContentListenNewsRecomendCell class] forCellWithReuseIdentifier:@"recomendContent"];
     [_contentCollectionView registerClass:[ZBBContentListenNewsOrdinaryCell class] forCellWithReuseIdentifier:@"ordinaryContent"];
      _contentCollectionView.backgroundColor = [UIColor whiteColor];
    
    
    
    
    
    
    
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _titleCollectionView) {
        
        return _titleSourceArray.count;
    }
    else
    {
        
        return _titleSourceArray.count;
    }
        
        
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _titleCollectionView) {
        
          NSString *string =  [_titleSourceArray[indexPath.row] objectForKey:@"tname"];
        return CGSizeMake(string.length * 20, 40);
    }
     else if (collectionView == _contentCollectionView)
     {
         
         return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
         
     }
   else
   {
       return CGSizeMake(0, 0);
   }
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _titleCollectionView) {
        
       ZBBListenNewsTitleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"title" forIndexPath:indexPath];
        cell.title =  [_titleSourceArray[indexPath.row] objectForKey:@"tname"];
        return cell;
    }
     else if (collectionView == _contentCollectionView)
     {
         ZBBContentListenNewsRecomendCell *recomendCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"recomendContent" forIndexPath:indexPath];
         ZBBContentListenNewsOrdinaryCell *ordinaryCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ordinaryContent" forIndexPath:indexPath];
         
         if (indexPath.row == 0) {
             recomendCell.urlString = _categoryId;
             return recomendCell;
         }
         else
         {
             
             ordinaryCell.dic = _titleSourceArray[indexPath.row];
             
             return ordinaryCell;
         }
         
         
         
         
         
     }
    
    
    
    
    
    
    
    return nil;
}

-(void)getData
{
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"推荐",@"tname" ,nil];
    _titleSourceArray = [NSMutableArray arrayWithObject:dic];
    NSString *urlTitleString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/tagsWithoutCover?categoryId=%@&contentType=album&device=android",_categoryId];
   // NSString *urlContentString = @"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=1&contentType=album&device=android&scale=2&version=4.3.14.3";
    
       [TwoToOne getDataByString:urlTitleString BodyString:nil WithDataBlock:^(id data) {
           NSArray *array = [data objectForKey:@"list"];
           for (NSDictionary *dic in array) {
               
               [_titleSourceArray addObject:dic];
           }
           
        
           
           [_contentCollectionView reloadData];
           [_titleCollectionView reloadData];
       }];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
